*****************************
** S-RLS Panel PS2 Survey **
** Cleaning Do File          **                                     
************************************************

clear all
set more off
set maxvar 10000
//ssc install missings 


use "${dir}/intermediate_public/panel_ps2_deidentified_merged.dta", clear

	

*****************************CLEANING***************************
**Changing all indicator variables to 0,1
quietly ds , has(type numeric)
	foreach var in `r(varlist)' {
		quietly count if !inlist(`var',.,1,2)
		if `r(N)'==0 {
			assert inlist(`var',.,1,2)==1
			local label1 :  label  `var' 1
			local label2 :  label  `var' 2
			if "`label1'"=="Yes" & "`label2'"=="No" {
				replace `var' = 0 if `var'==2 
				assert inlist(`var',.,1,0)
				lab define `var' 0 "No" 1 "Yes", replace 
				lab values `var' `var'
			} 
		}
	else {
		quietly count if !inlist(`var',.,1,2,-99)
		if `r(N)'==0 {
			assert inlist(`var',.,1,2,-99)==1
			local label1 :  label  `var' 1
			local label2 :  label  `var' 2
			local label9 : label `var' -99
			if "`label1'"=="Yes" & "`label2'"=="No" {
				replace `var' = 0 if `var'==2 
				assert inlist(`var',.,1,0,-99)
				lab define `var' 0 "No" 1 "Yes" -99 "`label9'", replace 
				lab values `var' `var'
				tab `var',mi nolab
			}
		}
	}
}

**Dropping numeric variables with 0 obs
foreach var of varlist _all {
	capture assert mi(`var')
	if !_rc {
		drop `var'
	}
}

**Remove hidden spaces from string variables
ds, has (type string)
local strvars `r(varlist)' 
foreach var of varlist `r(varlist)' {
	replace `var' = strtrim(`var')
}

**Droping all variables with only "." and all string variables with 0 obs
 missings dropvars, force
 
 *destring everything that can be destrung 
ds, has(type string) 
local strvars "`r(varlist)'"
destring `strvars', replace 

ds, has(type string) 
local strvars "`r(varlist)'"
foreach var of varlist `strvars' {
  replace `var' = strtrim(`var')
}

** Renaming variables to match survey doc
rename weekday current_day 
rename fr_confirm q103
rename fr_confirm_warning q103_warning
rename fr_confirm_warning_C1 q103_warning_C1
rename q109 q113
rename q110 q114
rename q5010a q510a
rename q5010b q510b

************************** Dropping Roster Variables **************************

drop memberage* memberage*x memberid* memberid*x filter_age filter_age_over10 filter_age_over18 filter_age_over18x filter_males filter_females filter_children_edu q511_counter_* member_index_text_ar_* member_index_text_en_* s5_index_* sum_children_edu sum_male_adult sum_female_adult sum_boys_under_18 sum_girls_under_18 sum_boys_10to16 sum_girls_10to16 sum_over18x sum_family_members calculated_member_age_* preround_index_modi_* gender_old_* gender_numi_* member_gender_* final_member_gender_* rid__premembers_* q401_sum q401_left_sum q401_calc q509 q510 previous_round_index_* roster_index_* index_mod_* final_member_index_* final_member_age_* age_int_old_* newhhmember_index_* new_member_indicatorz_* previous_age_* over12_* over18_* over18x_* over12andmale_* over12andfemale_* children_for_edu_* children_for_edu_join_*  current_members_* above_5and_under_* respondent_same_hhhead q404_calc_* member_age_* members_in_hh_* index_newmembers_1 members_sum_* boys_under_18_* girls_under_18_* male_18_* female_18_* boys_10to16_* girls_10to16_* six_to_17_* over10_* count_old_member_* count_left_member_* member_years_decimal_* new_member_indicator_* member_id_* new_member_indicatorx_* hhid_compin_* q407_* weekday_day current_day index_newmembers_* q401a* formdef_version

********************************************************************************

save "${dir}/intermediate_public/panel_ps2_cleaned_deidentified.dta", replace


